layui.define([ 'form','memberApi','layer' ], function(exports) {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form,el;
    var memberApi = layui.memberApi;
    var view = {
        config:{
            form:''
            ,memberTypeElement:''//用户类型存放的容器
            ,memberTypeValue:['','','']//用户类型的值
        },
        render: function(options){
            this.config = $.extend(this.config,options);
            this.$form = $(this.config.form);
            var layFilterValue = this.$form.attr("lay-filter");
            var id = this.$form.attr("id");
            if ($(this.config.form).length == 0) {
                console.log("用户注册表单未指定或不存在,form标签上的id和lay-filter必须相同");
                // Common.info("用户注册表单未指定或不存在,form标签上的id和lay-filter必须相同");
                return false;
            }

            if (layFilterValue != id) {
               console.log("form标签上的id和lay-filter必须相同");
               //Common.info("form标签上的id和lay-filter必须相同");
               return false;
            }

            if (this.config.memberTypeElement.length == 0) {
                console.log("未指定用户类型容器的选择器");
                return false;
            }

            Common.initKindedit(".kindEditor");

            this.initRegForm();
            this.initVerify();

            var that = this;
            form.on('submit(' + id + ')', function(data){
                id = that.$form.find("input[name='id']").val();
                if (id) {
                    memberApi.update(that.$form,function(){
                        Common.info("更新成功");
                    });
                } else {
                    memberApi.reg(that.$form,function(){
                        Common.info("注册成功");
                        location.href=Common.ctxPath + "/login.do";
                    });
                }
                return false;
            });
        },
        initRegForm() {
            memberApi.initMemberType(this.config);
        },
        initVerify() {//初始化表单中各个控件的验证
            var that = this;

            $(document).on("blur",this.config.form + " input[name='loginName']",function() {
                if ($(this).val() && $(this).val().length > 0) {
                    var result = memberApi.loginNameVerify($(this).val());
                    if (result.code == 0) {
                        layer.msg("用户名可以用");
                    } else {
                       layer.msg("用户名已被占用");
                    }
                }
            });
            $(document).on("blur",this.config.form + " input[name='domain']",function() {
                if ($(this).val() && $(this).val().length > 0) {
                    var result = memberApi.domainVerify($(this).val());
                    if (result.code == 0) {
                        layer.msg("域名可以用");
                    } else {
                       layer.msg("域名已被占用");
                    }
                }
            })
            //初始化验证码控件
            $(document).on("dblclick",this.config.form + " .verifyImg",function() {
                $(this).attr("src","/core/verify/image?token=reg&date=" + new Date().getTime());
            });
            $(document).on("blur",this.config.form + " .verifyCode",function() {
                $.post("/core/validVeirfyCode", {code : $(this).val(),token:'reg'}, function(data) {
                    if(data.code != 0) {
                        Common.alert(data.msg);
                    }
                });
            })
        }
    };
	exports('reg', view);
});